<?php
namespace app\admin\controller;

class Index extends Common
{
    public function index()
    {
        $username=session("admin")->username;
        return $this->fetch('index',compact("username"));
    }

    /**
     * 显示桌面
     */
    public function desk() {
        $data['view_count']=db("view_record")->count();
        $data['today_view_count']=db("view_record")->whereTime('create_time', 'today')->count();
        $data['contact_us_count']=db("contact_us")->count();
        $data['news_count']=db("news")->count();
      return $this->fetch('desk',compact("data"));
    }
    /**
     * 显示桌面
     */
    public function password() {
        if(request()->isAjax()) {
            $admin = session("admin");
            $oldPwd = input("oldPwd");
            $newPwd = input("newPwd");
            $newPwd1 = input("newPwd1");
            if (md5($oldPwd . $admin->salt)!= $admin->password) {
                return $this->renderError('原密码有误');
            }
            if (md5($newPwd .$admin->salt)== $admin->password) {
                return $this->renderError('新旧密码一致');
            }
            if (strlen($newPwd) < 6 || strlen($newPwd) > 20) {
                return $this->renderError('请输入6-20位字母或者数字新密码');
            }
            if ($newPwd != $newPwd1) {
                return $this->renderError('两次输入密码不一致');
            }
            $res=db("admin")->where(['id'=>$admin->id])->update(['password'=>md5($newPwd . $admin->salt)]);
            if($res){
                session("admin",null);
                return $this->renderSuccess('修改成功,请重新登录',url('login/index'));
            }
           return $this->renderError('修改失败');
        }
        return $this->fetch('password');
    }
      public function about() {
            $id=1;
          if(request()->isPost()){
              $data=input();
              unset($data['/admin/index/about_html']);
                  $res= db("about_us")->where(['id'=>$id])->update($data);
              if($res!==false){
                  return $this->renderSuccess('操作成功',url('index/about'));
              }
              return $this->renderError('操作失败');
          }
          $where['id']=$id;
          $info=db("about_us")
              ->where($where)
              ->find();
          return $this->fetch('about',compact("info"));
    }

    //服务案例
    public function servercase()
    {
        $list= db("server_case")->paginate();
        return $this->fetch('servercase',compact("list"));
    }
    public function edit()
    {
        if(request()->isPost()){
            $data=input();
            $insert['title']=$data['title'];
            $insert['status']=$data['status'];
            $insert['image']=$data['image'];
            $insert['sort']=$data['sort'];
            $insert['create_time']=time();
            if(input("id")){
                $res= db("server_case")->where(['id'=>$data['id']])->update($insert);
            }else{
                $res=db("server_case")->insert($insert);
            }
            if($res!==false){
                return $this->renderSuccess('操作成功',url('index/servercase'));
            }
            return $this->renderError('操作失败');
        }
        $where['id']=input("id");
        $info=db("server_case")
            ->where($where)
            ->find();
        return $this->fetch('edit',compact("info"));
    }
    public function server_del()
    {
        if(request()->isPost()){
            $id=input("id");
            $res= db("server_case")->where(['id'=>$id])->delete();
            if($res!==false){
                return   $this->renderSuccess('删除成功',url('index/servercase'));
            }
            return  $this->renderError('删除失败',url('index/servercase'));
        }
        return  $this->renderError('非法访问',url('index/servercase'));
    }
    //联系我们
    public function contact()
    {
        $list= db("contact_us")->order("id desc")->paginate(10);
        return $this->fetch('contact',compact("list"));
    }

    //删除联系我们
    public function contact_del()
    {
        $where['id']=input("id");
        $res= db("contact_us")->where($where)->delete();
        if($res){
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError("删除失败");
    }

    //联系我们
    public function view_record()
    {
        $list= db("view_record")->order("id desc")->paginate(10);
        return $this->fetch('view_record',compact("list"));
    }

    //删除联系我们
    public function view_record_del()
    {
        $where['id']=input("id");
        $res= db("view_record")->where($where)->delete();
        if($res){
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError("删除失败");
    }
}
